﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraEditors.Controls;

namespace QuangNgai.nhaplieu.hosotailieu
{
    public partial class frmTruongTinTaiLieu : DevExpress.XtraEditors.XtraForm
    {
        bool doubleClick = false;

        public frmTruongTinTaiLieu()
        {
            InitializeComponent();

        }

        private void frmTruongTinTaiLieu_Load(object sender, EventArgs e)
        {
            this.hsfieldtypeTableAdapter.Fill(this.lTQNDataSet.Hsfieldtype);
            this.hsfieldsTableAdapter.Fill(this.lTQNDataSet.Hsfields);

        }

        private void gvTruongTin_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            hsfieldsBindingSource.EndEdit();
            hsfieldsTableAdapter.Update(lTQNDataSet.Hsfields);
        }

        private void gvTruongTin_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                gvTruongTin_RowUpdated(sender, null);
            }
        }

        private void gcTruongTinHoSo_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.ButtonType == NavigatorButtonType.Remove)
            {
                e.Handled = true; // by-pass default handle
                if (MessageBox.Show("Bạn có muốn xóa trường tin này?", "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) ==
                  DialogResult.Yes)
                {

                    hsfieldsBindingSource.RemoveCurrent();
                    hsfieldsTableAdapter.Update(lTQNDataSet.Hsfields);
                }
            }
        }

        private void gvTruongTin_ShowingEditor(object sender, CancelEventArgs e)
        {
            //if (!doubleClick)
            //    e.Cancel = true;
            //else
            //    doubleClick = false;
        }

        private void gcTruongTinHoSo_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //GridControl grid = sender as GridControl;
            //ColumnView view = grid.FocusedView as ColumnView;
            //GridHitInfo hi = view.CalcHitInfo(e.Location) as GridHitInfo;
            //if (hi.HitTest == GridHitTest.RowCell)
            //    doubleClick = true;
        }

        private void gvTruongTin_ValidateRow(object sender, ValidateRowEventArgs e)
        {

        }

        private void gvTruongTin_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            if (gvTruongTin.FocusedColumn.Name == "colFullfieldname")
                if (e.Value.ToString() == "" || e.Value == null)
                {
                    e.Valid = false;
                }
        }

        private void gvTruongTin_InvalidValueException(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.DisplayError;
            e.ErrorText = "Tên trường tin không được trống";
            e.WindowCaption = "Lỗi nhập liệu";
        }
    }
}